<?php

declare(strict_types=1);

use Phinx\Migration\AbstractMigration;

final class OrderItemTableMigration extends AbstractMigration
{
    /**
     * Change Method.
     *
     * Write your reversible migrations using this method.
     *
     * More information on writing migrations is available here:
     * https://book.cakephp.org/phinx/0/en/migrations.html#the-change-method
     *
     * Remember to call "create()" or "update()" and NOT "save()" when working
     * with the Table class.
     */
    public function change(): void
    {
        $table = $this->table('order_items', [
            'charset' => 'utf8mb4',
            'collation' => 'utf8mb4_general_ci'
        ]);

        $table->addColumn('order_id', 'integer', [
            'limit' => 11,
            'null' => false,
            'comment' => '订单ID'
        ])
        ->addColumn('goods_id', 'integer', [
            'limit' => 11,
            'null' => false,
            'comment' => '商品ID'
        ])
        ->addColumn('goods_ku_id', 'integer', [
            'limit' => 11,
            'null' => true,
            'comment' => '商品SKU ID'
        ])
        ->addColumn('goods_title', 'string', [
            'limit' => 255,
            'null' => false,
            'comment' => '商品标题（下单时的快照）'
        ])
        ->addColumn('goods_image', 'string', [
            'limit' => 255,
            'null' => true,
            'comment' => '商品图片（下单时的快照）'
        ])
        ->addColumn('sku_specs', 'text', [
            'null' => true,
            'comment' => 'SKU规格信息（JSON格式，下单时的快照）'
        ])
        ->addColumn('sku_spec_names', 'text', [
            'null' => true,
            'comment' => 'SKU规格名称（JSON格式，如：{"颜色":"红色","尺寸":"L"}）'
        ])
        ->addColumn('unit_price', 'decimal', [
            'precision' => 10,
            'scale' => 2,
            'null' => false,
            'default' => 0.00,
            'comment' => '单价（下单时的价格）'
        ])
        ->addColumn('quantity', 'integer', [
            'limit' => 11,
            'null' => false,
            'default' => 1,
            'comment' => '购买数量'
        ])
        ->addColumn('total_price', 'decimal', [
            'precision' => 10,
            'scale' => 2,
            'null' => false,
            'default' => 0.00,
            'comment' => '小计金额'
        ])
        ->addColumn('discount_amount', 'decimal', [
            'precision' => 10,
            'scale' => 2,
            'null' => false,
            'default' => 0.00,
            'comment' => '优惠金额'
        ])
        ->addColumn('final_price', 'decimal', [
            'precision' => 10,
            'scale' => 2,
            'null' => false,
            'default' => 0.00,
            'comment' => '最终价格（单价*数量-优惠金额）'
        ])
        ->addColumn('refund_status', 'integer', [
            'limit' => 2,
            'null' => false,
            'default' => 0,
            'comment' => '退款状态：0未退款，1退款中，2已退款，3退款失败'
        ])
        ->addColumn('refund_amount', 'decimal', [
            'precision' => 10,
            'scale' => 2,
            'null' => false,
            'default' => 0.00,
            'comment' => '退款金额'
        ])
        ->addColumn('refund_time', 'integer', [
            'limit' => 11,
            'null' => true,
            'comment' => '退款时间'
        ])
        ->addColumn('refund_reason', 'string', [
            'limit' => 255,
            'null' => true,
            'comment' => '退款原因'
        ])
        ->addColumn('created_at', 'integer', [
            'limit' => 11,
            'null' => false,
            'comment' => '创建时间'
        ])
        ->addColumn('updated_at', 'integer', [
            'limit' => 11,
            'null' => false,
            'comment' => '更新时间'
        ])
        ->addIndex(['order_id'])
        ->addIndex(['goods_id'])
        ->addIndex(['goods_ku_id'])
        ->addIndex(['refund_status'])
        ->create();
    }
}
